from rich.console import Console
from rich.prompt import Prompt
from rich.pretty import Pretty
from rich.panel import Panel
from rich import print


def ui_pretty_print(d):
    pretty = Pretty(d, expand_all=True)
    panel = Panel(pretty)
    print(panel)


class RUi:
    def __init__(self):
        self._console = Console(width=100)

    def cls(self):
        self._console.clear()

    def ui_print_info(self, info: str):
        line = '[bold yellow]' + info + '[/bold yellow]'
        self._console.print(line)

    def ui_select(self, d, prompt: str = 'Select item'):
        ls = list(d)
        self._console.clear()
        self._console.rule(prompt)
        for i in range(len(ls)):
            index = '[bold]' + str(i) + '[/bold]'
            key = '[bold yellow]' + ls[i] + '[/bold yellow]'
            value = ''
            if type(d) is dict:
                value = d[ls[i]]
            o = '{:<10} {:>50} {:>60}'.format(index, key, value)
            self._console.print(o)
        c = [str(i) for i in range(len(ls))]
        c.append('')
        id = Prompt.ask('Enter index', choices=c)
        if id == '':
            return None
        else:
            return ls[int(id)]
